package com.clss.emergencycall.module.imui.util;

import android.content.Context;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

/**
 * 作者：Rance on 2016/12/20 16:41
 * 邮箱：rance935@163.com
 */
public class Utils {
    /**
     * dp转dip
     *
     * @param context
     * @param dp
     * @return
     */
    public static int dp2px(Context context, float dp) {
        float density = context.getResources().getDisplayMetrics().density;
        return (int) (dp * density + 0.5F);
    }

    /**
     * 文本中的emojb字符处理为表情图片
     *
     * @param context
     * @param tv
     * @param source
     * @return
     */
//    public static SpannableString getEmotionContent(final Context context, final TextView tv, String source) {
//        SpannableString spannableString = new SpannableString(source);
//        Resources res = context.getResources();
//
//        String regexEmotion = "\\[([\u4e00-\u9fa5\\w])+\\]";
//        Pattern patternEmotion = Pattern.compile(regexEmotion);
//        Matcher matcherEmotion = patternEmotion.matcher(spannableString);
//
//        while (matcherEmotion.find()) {
//            // 获取匹配到的具体字符
//            String key = matcherEmotion.group();
//            // 匹配字符串的开始位置
//            int start = matcherEmotion.start();
//            // 利用表情名字获取到对应的图片
//            Integer imgRes = EmotionUtils.EMOTION_STATIC_MAP.get(key);
//            if (imgRes != null) {
//                // 压缩表情图片
//                int size = (int) tv.getTextSize() * 13 / 8;
//                Bitmap bitmap = BitmapFactory.decodeResource(res, imgRes);
//                Bitmap scaleBitmap = Bitmap.createScaledBitmap(bitmap, size, size, true);
//
//                ImageSpan span = new ImageSpan(context, scaleBitmap);
//                spannableString.setSpan(span, start, start + key.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//            }
//        }
//        return spannableString;
//    }

    /**
     * 返回当前时间的格式为 yyyyMMddHHmmss
     *
     * @return
     */
    public static String getCurrentTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(System.currentTimeMillis());
    }

    //毫秒转秒
    public static String long2String(long time) {
        //毫秒转秒
        int sec = (int) time / 1000;
        int min = sec / 60;    //分钟
        sec = sec % 60;        //秒
        if (min < 10) {    //分钟补0
            if (sec < 10) {    //秒补0
                return "0" + min + ":0" + sec;
            } else {
                return "0" + min + ":" + sec;
            }
        } else {
            if (sec < 10) {    //秒补0
                return min + ":0" + sec;
            } else {
                return min + ":" + sec;
            }
        }
    }

    /**
     * 毫秒转化时分秒毫秒
     *
     * @param ms
     * @return
     */
    public static String formatTime(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;

        StringBuffer sb = new StringBuffer();
        if (day > 0) {
            sb.append(day + "d");
        }
        if (hour > 0) {
            sb.append(hour + "h");
        }
        if (minute > 0) {
            sb.append(minute + "′");
        }
        if (second > 0) {
            sb.append(second + "″");
        }
        return sb.toString();
    }

    public static class sdcard {
        private final static String folder = "/cj"; // 根目录
        private final static String img = "/img"; // 图片目录
        private final static String video = "/video"; // 视频目录
        private final static String audio = "/audio"; // 音频目录
        private final static String download = "/download"; // 下载目录
        private final static String tmp = "/tmp"; // 临时目录
        private final static String log = "/log"; // 日志
        private final static String headfile = download + "/headfile"; //头像文件

        public static String getRootPath() {
            File file = new File(PhoneUtil.getSdcardDirectory() + folder);
            if (!file.exists()) {
                file.mkdirs();
            }

            return PhoneUtil.getSdcardDirectory() + folder;
        }

        public static String getImg() {
            File file = new File(getRootPath() + img);
            if (!file.exists()) {
                file.mkdirs();
            }

            return getRootPath() + img;
        }

        public static String getVideo() {
            File file = new File(getRootPath() + video);
            if (!file.exists()) {
                file.mkdirs();
            }

            return getRootPath() + video;
        }

        public static String getAudio() {
            File file = new File(getRootPath() + audio);
            if (!file.exists()) {
                file.mkdirs();
            }
            return getRootPath() + audio;
        }

        public static String getTmp() {
            File file = new File(getRootPath() + tmp);
            if (!file.exists()) {
                file.mkdirs();
            }

            return getRootPath() + tmp;
        }

        public static String getDownload() {
            File file = new File(getRootPath() + download);
            if (!file.exists()) {
                file.mkdirs();
            }

            return getRootPath() + download;
        }

        public static String getLog() {
            File file = new File(getRootPath() + log);
            if (!file.exists()) {
                file.mkdirs();
            }

            return getRootPath() + log;
        }

        public static String getHeadFile() {
            File file = new File(getRootPath() + headfile);
            if (!file.exists()) {
                file.mkdirs();
            }

            return getRootPath() + headfile;
        }

        public static void mkdirs() {
            File file = new File(getRootPath());
            if (!file.exists()) {
                file.mkdirs();
            }

			/*file = new File(getImg());
			if(!file.exists())
				file.mkdirs();

			file = new File(getImg());
			if(!file.exists())
				file.mkdirs();

			file = new File(getTmp());
			if(!file.exists())
				file.mkdirs();

			file = new File(getDownload());
			if(!file.exists())
				file.mkdirs();*/
        }
    }
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
    public static ArrayList<HashMap<String, Object>> jsa2list(String s) {
        ArrayList<HashMap<String, Object>> dataList = new ArrayList<>();
        JSONArray jsonArray;
        try {
            jsonArray = new JSONArray(s);
            for (int i = 0; i < jsonArray.length(); i++) {
                dataList.add(jso2map(jsonArray.get(i).toString()));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return dataList;
    }

    public static HashMap<String, Object> jso2map(String s) {
        JSONObject jsonObjTemp;
        HashMap<String, Object> map = new HashMap<>(16);
        if (s == null) {
            return null;
        }
        try {
            jsonObjTemp = new JSONObject(s);
            Iterator ite = jsonObjTemp.keys();
            while (ite.hasNext()) {
                String key = ite.next().toString();
                String value = jsonObjTemp.get(key).toString();
                map.put(key, value);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return map;
    }
    public static boolean isNull(Object object){
        return object == null || object.toString().trim().equals("");
    }
    public static boolean isNotNull(Object object){
        return object != null && !object.toString().trim().equals("");
    }
}
